#include <buola/buola.h>

#if defined BUOLA_HAVE_FREENECT && !defined BUOLA_HAVE_OPENNI && !defined BUOLA_HAVE_OPENNI2

#include "ckinectdriver_freenect.h"

#include <sys/syscall.h>

namespace buola { namespace cvi {

CKinectDriver::CKinectDriver()
{
    if(freenect_init(&mContext,nullptr)<0)
        throw XHardware("can't initialize libfreenect context");
    freenect_set_log_level(mContext,FREENECT_LOG_DEBUG);
    freenect_select_subdevices(mContext,FREENECT_DEVICE_CAMERA);
    
    CreateDeviceList();
}

CKinectDriver::~CKinectDriver()
{
    freenect_shutdown(mContext);
}

void CKinectDriver::CreateDeviceList()
{
    //enumerate devices
    int lNumDevices=freenect_num_devices(mContext);
    
    for(int i=0;i<lNumDevices;i++)
        mDevices.emplace_back(new CKinectDevice(mContext,i));

    msg_info() << "created device list: " << lNumDevices << "\n";
}

CKinectDriver &get_kinect_driver()
{
    static CKinectDriver lDriver;
    
    return lDriver;
}

/*namespace cvi*/ } /*namespace buola*/ }

#endif
